using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Xml;
using MySQLStudio.GUI.Controls;
using MySQLStudio.GUI.Properties;

namespace MySQLStudio.GUI.Controls
{
	static class Highlighters
	{
        private static MySQLStudio.GUI.Controls.HighLightDescriptorCollection sql;


        public static MySQLStudio.GUI.Controls.HighLightDescriptorCollection Sql
		{
			get
			{
				if (sql == null) {
					XmlDocument lexer = new XmlDocument();
                    lexer.LoadXml(Resources.SqlLexer);
					sql = GetHighlightingStrategy("SQL", lexer);
				}
				return sql;
			}
		}


        private static HighLightDescriptorCollection GetHighlightingStrategy(string name, XmlDocument doc)
		{
            MySQLStudio.GUI.Controls.HighLightDescriptorCollection highlighter = new MySQLStudio.GUI.Controls.HighLightDescriptorCollection();
            
			XmlNodeList nodes = doc.DocumentElement.GetElementsByTagName("RuleSet");
			foreach (XmlElement element in nodes) 
            {
                foreach(XmlNode node in element)
                {
                    XmlNode n = GetAttributeNode("color",node);
                    
                    if(n != null)
                    {
                        Color color = Color.FromName(n.Value);
                        
                        foreach(XmlNode innerNode in node)
                        {
                            XmlNode innerNodeAttributeNode = GetAttributeNode("word",innerNode);
                            if(innerNodeAttributeNode != null)
                            {
                                String value = innerNodeAttributeNode.Value;
                                Console.Out.WriteLine("value " + value);
                                highlighter.Add(new HighlightDescriptor(value, color, null, DescriptorType.Word, DescriptorRecognition.WholeWord, false));
                            }
                        }
                    }
                }
                //ReservedWords
                //AccessKeywords
                //OperatorKeywords
			}

            return highlighter;
		}
        private static XmlNode GetAttributeNode(String name, XmlNode node)
        {
            XmlNode retVal = null;
            XmlAttributeCollection attribute = node.Attributes;
            if(attribute != null)
                retVal = attribute.GetNamedItem(name);

            return retVal;
        }
        private static void TranslateNodeToRules(XmlNode node, HighLightDescriptorCollection collection)
        { 

           // foreach()
        }
	}
    
}
